﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php

//patikrinti ar nera id is GET
if (isset($_GET['vartotojasid'])) {
$edit = true;
};

//patikrint ar nera tokio vartotojo, 
// Jeigu forma nauja ir submitinta - tikrina, jeigu ne - ne (logichno?)
	if (($edit == FALSE) && isset($_POST['Submit']) && ($_POST['Submit'])!=null ){
		$sql = "SELECT * ";
		$sql .= "FROM `vartotojas` ";
		$sql .= "WHERE `vartotojas`.`vartvardas` = ";
		$sql .= "'". ($_POST['vardas']). "'";
		//procesinam query	
				$result = mysql_query($sql, $connection);
				$tmp = mysql_fetch_array($result);
			if (($tmp['vartvardas'])!= NULL) {
				// Success!
				$message .= "</br>Toks vartotojas jau yra </br>";
		//----------------------------------------------------------------------//
		//         DADET Table su useriu kuris turi toki bylos NR.				//
		//----------------------------------------------------------------------//
			} else {
				// Display error message.
				$message .= "<p>Naujas vartotojas</p>";
				//echo "<p>" . mysql_error() . "</p>";
			};
	} ;


// Jeigu yra GET bet nera POST pildom forma is duombazes
	if ($edit && !isset($_POST['Submit'])) {
		$message = "</br> Na ir kuom tu ne patenkintas? </br>";
	//--------------------------------------------------------------------------//
	//  query pagal userid, zpildyk kintamasias formoi, pakeis knopkes uzrasus	//
	//--------------------------------------------------------------------------//	
		
		$sql = "SELECT * ";
		$sql .= "FROM `vartotojas` ";
		$sql .= "WHERE `vartotojas`.vartotojasid = ";
		$sql .= "'". ($_GET['vartotojasid']). "'";
		//procesinam query	
				$result = mysql_query($sql, $connection);
				$tmp = mysql_fetch_array($result);
				$vardas = $tmp['vartvardas'];
				
	} else {
		//$message = "</br> Userid is GET nera. Kursim nauja user'i. </br>";
	};

  //=========================================================================//
 //    					PADARYT NORMALU TIKRINIMA							//
//=========================================================================//
	
	
//patikrint ar visi duomenys ivesti, jeigu ivesti imest i kintamasias ir suprocesinti
	if (!isset($_POST['Submit']) && ($_POST['Submit'])==null ){
		//POST nera
		//$message .= "</br> Forma ne POSTinta";
	} else {
		//POST yra. Tikrinam reiksmes ir imetam kintamasias
		//$message = "We have a POST </br>";
		if (!isset($_POST['vardas']) or ($_POST['vardas'])==null ){
			$message .= " Nera Vartotojo vardo </br>";
		} else {
			$vardas = $_POST['vardas'];
		};
		if (!isset($_POST['password']) or ($_POST['password'])==null ){
			$message .= " But we don't have password! </br>";
		} else {
			$password = $_POST['password'];
		};
		
		
		// rasom query naujam vartotojui ir seno updatenimui
		if ($edit == FALSE) {
	// Naujas vartotojas
		$sql = "INSERT INTO `vartotojas` (`vartvardas`, `password`) ";
		$sql .= "VALUES (";
		$sql .= "'". $vardas . "', ";
		$sql .= "'". sha1($password) . "' ";
		$sql .= ");";
		//echo "SQL naujam: ".$sql; //viskas OK
		
		$result = mysql_query($sql, $connection);
			if ($result) {
				// Success!
				$temp = mysql_insert_id();
				
//---------------------------------------------------------------------------------------------//
//         Pakeisk naujas klientas.php
//----------------------------------------------------------------------------------------------//
				$to = "tautybe.php?new=1&userid=". $temp;
				//redirect_to($to);
				//$message .= "</br>Client added Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Subject creation failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		} else {
		//Esamo redagavimas
		$sql = "UPDATE `vartotojas` SET";
		$sql .= " `vartvardas` =";
		$sql .= "'". $vardas . "', ";
		$sql .= " `password` =";
		$sql .= "'". sha1($password) . "' ";
		$sql .= " WHERE `userid` = ";
		$sql .= ($_GET['vartotojasid']);
		$sql .= " LIMIT 1;";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
				$to = "summary.php?userid=" . $_GET['userid'];
				//redirect_to($to);
				// Success!
				//$message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		// echo "SQL esamam: ".$sql; //HOORAY, veikia!!!!
		};
	};

	


	
	
?>

			
					<article>
						<h2><?php 
						if ($edit) { echo "Esamo vartotojo redagavimas";} 
						else { echo "Naujo vartotojo ivedimas";};
						?></h2>
						<p>Pagrindiniai duomenys</p>
						<br>
						<?php echo $message;
						?>
					  <form autocomplete="off" action="naujas_vartotojas.php<?php 
					  if ($edit) {echo "?vartotojasid=" . $_GET['vartotojasid'];};
					  ?>" method="post" name="search" accept-charset="utf-8">
					    <p>
					    </p>
					    <table   border="1">
					      <tr>
					        <td>Vartotojo vardas</td>
					        <td width="150"><input name="vardas" type="text" value="<?php
							echo $vardas;
							?>" required/></td>
					        <td>Slaptažodis</td>
					        <td width="150"><input name="password" type="password" value="" required /></td>
							
				          </tr>
						  
				        </table>
					    <br>
					    <p>
					      <input type="reset" name="Reset" id="button" value="Valyti laukus"><input name="Submit" type="submit" value="<?php if ($edit) {echo "Redaguoti";} else {echo "Pridėti vartotoją";};?>" /> 
					    </p>
					  </form>
 
			</article>
            
<?php require("includes/footer.php"); ?>